#include <iostream>
#include <pthread.h>
#include <vector>
#include <cstring>
#include <cstdio>
#include <cassert>
#include <unistd.h>
using namespace std;

class ThreadData
{
public:
    pthread_t tid;
    char name[64];
};
#define NUM 10
void *start_routine(void *arg)
{
    ThreadData *ptd = static_cast<ThreadData *>(arg);
    int cnt = 10;
    while (cnt--)
    {
        cout << ptd->name << endl;
        sleep(1);
    }
    // delete ptd;
    
    return nullptr;
}
int main()
{
    // 创建一批线程
    vector<ThreadData *> threads;
    for (int i = 0; i < NUM; ++i)
    {
        ThreadData *ptd = new ThreadData();
        snprintf(ptd->name, sizeof(ptd->name), "thread %d", i);
        int n = pthread_create(&ptd->tid, NULL, start_routine, ptd);
        assert(n == 0);
        threads.push_back(ptd);
    }
    // while (true)
    // {
    //     cout << "main thread" << endl;
    //     sleep(1);
    // }
    for (auto &td : threads)
    {
        void *ret = nullptr;
        pthread_join(td->tid, &ret);
        cout << "退出成功 " << (long long)ret << endl;
        delete td;
    }
    cout << "main函数退出" << endl;
    return 0;
}